package com.ruoyi.web.controller.api;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.api.domain.AppManage;
import com.ruoyi.api.service.IAppManageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 应用管理Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/api/appManage")
public class AppManageController extends BaseController
{
    @Autowired
    private IAppManageService appManageService;

    /**
     * 查询应用管理列表
     */
    @PreAuthorize("@ss.hasPermi('api:appManage:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppManage appManage)
    {
        startPage();
        List<AppManage> list = appManageService.selectAppManageList(appManage);
        return getDataTable(list);
    }

    /**
     * 导出应用管理列表
     */
    @PreAuthorize("@ss.hasPermi('api:appManage:export')")
    @Log(title = "应用管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppManage appManage)
    {
        List<AppManage> list = appManageService.selectAppManageList(appManage);
        ExcelUtil<AppManage> util = new ExcelUtil<AppManage>(AppManage.class);
        util.exportExcel(response, list, "应用管理数据");
    }

    /**
     * 获取应用管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:appManage:query')")
    @GetMapping(value = "/{appId}")
    public AjaxResult getInfo(@PathVariable("appId") Long appId)
    {
        return success(appManageService.selectAppManageByAppId(appId));
    }

    /**
     * 新增应用管理
     */
    @PreAuthorize("@ss.hasPermi('api:appManage:add')")
    @Log(title = "应用管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppManage appManage)
    {
        return toAjax(appManageService.insertAppManage(appManage));
    }

    /**
     * 修改应用管理
     */
    @PreAuthorize("@ss.hasPermi('api:appManage:edit')")
    @Log(title = "应用管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppManage appManage)
    {
        return toAjax(appManageService.updateAppManage(appManage));
    }

    /**
     * 删除应用管理
     */
    @PreAuthorize("@ss.hasPermi('api:appManage:remove')")
    @Log(title = "应用管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{appIds}")
    public AjaxResult remove(@PathVariable Long[] appIds)
    {
        return toAjax(appManageService.deleteAppManageByAppIds(appIds));
    }

    /**
     * 生成新的应用密钥
     */
    @PreAuthorize("@ss.hasPermi('api:appManage:edit')")
    @Log(title = "应用管理", businessType = BusinessType.UPDATE)
    @PutMapping("/regenerateKey/{appId}")
    public AjaxResult regenerateSecretKey(@PathVariable Long appId)
    {
        int result = appManageService.regenerateSecretKey(appId);
        if (result > 0) {
            // 返回更新后的应用信息，包含新的密钥
            AppManage appManage = appManageService.selectAppManageByAppId(appId);
            AjaxResult ajaxResult = success(appManage);
            ajaxResult.put("msg", "密钥生成成功");
            return ajaxResult;
        }
        return error("密钥生成失败");
    }
}
